#!/usr/bin/env bash
# Copyright (c) 2021 maminjie <maminjie2@huawei.com>
# SPDX-License-Identifier: MulanPSL-2.0

method_def listsig findsig findsigowner findsigpkg findmaintainer

# sig_find sigyaml pkg
sig_find() {
    local sig=$(echo "$1" | grep -E "\- name|/$2\$" | grep -B1 -E "/$2\$" | head -n 1 | grep "\- name" |\
        awk -F ':' '{print $2}' | awk '{print $1}')
    echo "$sig"
}

# sig_get
sig_get() {
    curl -s -X GET "https://gitee.com/openeuler/community/raw/master/sig/sigs.yaml"
}

usage_findsig() {
    echo "findsig (fsig): Find the sig group of packages"
    echo ""
    echo "usage:"
    echo "    ${PROG} fsig PACKAGES         # package list"
    echo "    ${PROG} fsig FILE             # list file"
    echo ""
    echo "example:"
    echo "    ${PROG} fsig \"pkgship python3 a b c\""
    echo ""
}

alias_def findsig fsig
# do_findsig pkgs
# do_findsig file
do_findsig() {
    if [ $# -ne 1 ]; then
        usage_findsig; exit
    fi
    local pkgs=$(get_list "$1")
    local sigyaml="$(sig_get)"
    {
    printf "%s|%s\n" "SIG" "PKG"
    printf "%s|%s\n" "--------" "--------"
    for pkg in $pkgs; do
        local sig=$(sig_find "$sigyaml" "$pkg")
        if [ -z "$sig" ]; then
            sig="Not-Found"
        fi
        printf "%s|%s\n" "$sig" "$pkg"
    done | sort -nk1
    } | format_column '|'
}


# sig_find_owner sig
sig_find_owner() {
    local signame=$1
    local sigowner=$(curl -s -X GET https://gitee.com/openeuler/community/raw/master/sig/"$signame"/OWNERS)
    local owners=$(echo "$sigowner" | grep '^\s*-\s*' | sed 's/^\s*-\s*//')
    if [ -z "$owners" ]; then
        owners="Not-Found"
    fi
    echo "$owners"
}

usage_findsigowner() {
    echo "findsigowner (fsigo): Find the owner of sig group"
    echo ""
    echo "usage:"
    echo "      ${PROG} fsigo SIGNAME"
    echo ""
}

alias_def findsigowner fsigo
# do_findsigowner sig
do_findsigowner() {
    if [ $# -ne 1 ]; then
        usage_findsigowner; exit
    fi
    sig_find_owner "$1"
}

usage_listsig() {
    echo "listsig (sigs): List all sigs"
    echo ""
    echo "usage:"
    echo "    ${PROG} sigs"
    echo ""
}

alias_def listsig sigs
# do_listsig
do_listsig() {
    sig_get | grep -E '^-\s*name:' | sed 's/^-\s*name:\s*//g'
}

# sig_find_package sig
sig_find_package() {
    local sig="$1"
    local pkgs=$(sig_get | sed 's/^\s*-\?\s*//g' | grep -v "repositories:" | awk -F '\n' '{
        if ($1 ~ /name:\s*'${sig}'/) {
            flag = 1
        } else if (flag == 1) {
            if ($1 ~ /name:/) { exit } else { print $1 }
        }
    }')
    echo "$pkgs"
}

usage_findsigpkg() {
    echo "findsigpkg (fsigp): Find the packages of sig"
    echo ""
    echo "usage:"
    echo "    ${PROG} fsigp SIGNAME"
    echo ""
}

alias_def findsigpkg fsigp
# do_findsigpkg sig
do_findsigpkg() {
    if [ $# -ne 1 ]; then
        usage_findsigpkg; exit
    fi
    sig_find_package "$1"
}

# sig_find_maintainer sig
sig_find_maintainer() {
    local owners=$(sig_find_owner "$1")
    if [ "$owners" != "Not-Found" ]; then
        local maintainer=""
        for ow in $owners; do
            if [ -z "$maintainer" ]; then
                maintainer="$ow"
            else
                maintainer="$maintainer,$ow"
            fi
        done
        echo "$maintainer"
    else
        echo "$owners"
    fi
}

usage_findmaintainer() {
    echo "findmaintainer (fmtr): Find the maintainers of packages"
    echo ""
    echo "usage:"
    echo "    ${PROG} fmtr PACKAGES         # package list"
    echo "    ${PROG} fmtr FILE             # list file"
    echo ""
}

alias_def findmaintainer fmtr
# do_findmaintainer pkgs
# do_findmaintainer file
do_findmaintainer() {
    if [ $# -ne 1 ]; then
        usage_findmaintainer; exit
    fi
    local pkgs=$(get_list "$1")
    local sigyaml="$(sig_get)"
    {
    printf "%s|%s|%s\n" PKG SIG MAINTAINER
    printf "%s|%s|%s\n" "--------" "--------" "--------"
    for pkg in $pkgs; do
        local sig=$(sig_find "$sigyaml" "$pkg")
        local maintainer="Not-Found"
        if [ -z "$sig" ]; then
            sig="Not-Found"
        else
            maintainer=$(sig_find_maintainer "$sig")
        fi
        printf "%s|%s|%s\n" "$pkg" "$sig" "$maintainer"
    done | sort -nk1
    } | format_column '|'
}
